import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

abstract class BaseViewModel<State : Any, Event>(
    initialState: State
) : ViewModel() {
    private val _state = MutableStateFlow(initialState)

    protected var viewState: State = initialState
        set(value) {
            field = value
            _state.value = value
        }

    abstract fun obtainEvent(event: Event)

    protected fun updateState(reducer: (State) -> State) {
        viewState = reducer(viewState)
    }

    fun viewStates(): StateFlow<State> = _state.asStateFlow()
}